const chalk = require('chalk')
const idate = require('../utils/idate')

function toMethod (type) {
  switch (type.toLowerCase()) {
    case 'get': return chalk.cyan(type)
    case 'post': return chalk.yellow(type)
    case 'del': return chalk.red(type)
    case 'put': return chalk.blue(type)
    default: return type
  }
}

function toStatus (val) {
  switch (val + '') {
    case '200': return chalk.green(val)
    case '404': return chalk.red(val)
    case '505': return chalk.red(val)
    default: return chalk.gray(val)
  }
}

module.exports = async function log (ctx, next) {
  const start = Date.now()

  await next()

  const ms = Date.now() - start

  const info = `[${idate().format('MM-DD HH:mm:ss.SSS')}] ${toMethod(ctx.method)} ${toStatus(ctx.status)} ${ctx.url} - ${chalk.magenta(ms)} ms`

  console.log(info)
}
